package com.ficus.road.maintain.handler.smartdecision;

import com.ficus.road.maintain.core.model.smartnew.SmartDecisionExcelVO;
import com.ficus.road.maintain.core.model.smartnew.SmartTable;
import com.ficus.road.maintain.core.utils.JsonUtils;
import com.ficus.road.maintain.handler.smartdecision.interfaces.SmartDecisionMerger;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author xiawen yang
 * @date 2022/8/11 下午3:52
 */
@Component
public class ReserveMerger implements SmartDecisionMerger {
    @Override
    public List<SmartTable> merge(List<SmartTable> smartTables) {
        List<SmartTable> res = new ArrayList<>();
        Map<String, List<SmartTable>> roadNameGroup = smartTables.stream().collect(Collectors.groupingBy(SmartTable::getRoadName));
        roadNameGroup.forEach((roadName, list) -> {
            SmartTable smartTable = JsonUtils.deepCopy(list.get(0), SmartTable.class);
            double startStake = list.stream().mapToDouble(SmartDecisionExcelVO::getStartStake).min().orElse(0);
            smartTable.setStartStake(startStake);
            double endStake = list.stream().mapToDouble(SmartDecisionExcelVO::getEndStake).max().orElse(0);
            smartTable.setEndStake(endStake);
            res.add(smartTable);
        });
        return res;
    }
}
